package com.phoenix.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class DateTimeUtil implements Serializable {

    public static Date parseToDate(String stringDate){
        if (StringUtils.isBlank(stringDate)){
            return new Date();
        }
        List<String> pattens=Stream.of(
                "yyyy年MM月dd日",
                "yy-MM",
                "yyyy-MM",
                "yyyy-MM-dd",
                "yyyy/MM/dd",
                "yyyy-MM-dd HH:mm:ss",
                "yyyy-MM-dd HH:mm",
                "yyyy-MM-dd HH"
        ).collect(Collectors.toList());
        for (String patten:pattens) {
            SimpleDateFormat df=new SimpleDateFormat(patten);
            try {
                return  df.parse(stringDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        log.error("转换日期发生异常，没有适配的pattern。日期为：{}",stringDate);
        return null;
    }
}
